package org.tang.springcloudvv.product.controller;

import com.alibaba.druid.support.json.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.tang.springcloudvv.base.Result;
import org.tang.springcloudvv.persistence.base.model.Product;
import org.tang.springcloudvv.product.feign.Springcloudvvuser;
import org.tang.springcloudvv.product.service.IProductService;
import org.tang.springcloudvv.product.service.ProductService;

import java.util.List;

/**
 * <p><em>Created on 2021/12/1 5:44 下午</em></p>
 * 商品相关
 * @author fuzhongtang
 * @since 1.0.1
 */
@Slf4j
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private Springcloudvvuser springcloudvvuser;

    @PostMapping("/product/save")
    public Result save(@RequestParam("name") String name,@RequestParam("price") Integer price,@RequestParam("times") Integer times)
    {
        int iSave = 0;
        for (int i = 0; i < times; i++) {
            iSave = iSave+productService.save(name+i,price+i);
        }
        return Result.of(HttpStatus.OK.value(),HttpStatus.OK.name(),"iSave="+iSave);
    }

    @PostMapping("/product/select")
    public Result select()
    {
        Result result = new Result(HttpStatus.OK.value(),"");
        try {
            Result result_ = springcloudvvuser.getUser("tang");
            result.setMessage(JSONUtils.toJSONString(result_));
        }
        catch (Exception e)
        {
            log.error("",e);
            result.setMessage("error:"+JSONUtils.toJSONString(e));
        }

        List<Product> list = productService.select();
        result.setData(list);
        return result;
    }
    @PostMapping("/product/getUser")
    public Result getUser()
    {
        Result result = new Result(HttpStatus.OK.value(),"");
        try {
            Result result_ = springcloudvvuser.getUser("tang");
            return result_;
        }
        catch (Exception e)
        {
            log.error("",e);
            result.setMessage("error:"+JSONUtils.toJSONString(e));
        }

        return result;
    }
}
